package com.dalianpai.gulimall.seckill.controller;

import com.dalianpai.common.utils.R;
import com.dalianpai.gulimall.seckill.service.SeckillService;
import com.dalianpai.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author WGR
 * @create 2020/8/18 -- 19:51
 */
@Controller
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckillSkus")//活动当前可以秒杀的商品列表
    public R getCurrentSeckillSkus(){
        List<SecKillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")//检查某个skuid是否对应秒杀信息，如果不是秒杀商品那么返回的就是null
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(to);
    }

    //TODO 上架秒杀商品时候设置ttl，秒杀后续流程，简化了收货地址确认等
    @GetMapping("/kill")//秒杀请求处理逻辑
    public String secKill(@RequestParam("killId") String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model){
        String orderSn = seckillService.kill(killId,key,num);//秒杀商品id，随机码，秒杀数量
        System.out.println(orderSn);
        // 1.LoginUserInterceptor中判断是否登录
        //秒杀成功后，返回订单号，在页面中使用订单号
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
